package com.mobi.Survey.lib.facades;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.graphics.Color;

import com.mobi.Survey.lib.charts.CategorySeries;
import com.mobi.Survey.lib.charts.PointStyle;
import com.mobi.Survey.lib.charts.SimpleSeriesRenderer;
import com.mobi.Survey.lib.charts.XYMultipleSeriesDataset;
import com.mobi.Survey.lib.charts.XYMultipleSeriesRenderer;
import com.mobi.Survey.lib.charts.XYSeries;
import com.mobi.Survey.lib.charts.XYSeriesRenderer;
import com.mobi.Survey.lib.model.AggregateResultPojo;
import com.mobi.Survey.lib.utils.Device;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * AbstractChart.java
 * 
 */
public abstract class AbstractChart implements IChart
{
	private AggregateResultPojo graphData;

	public AbstractChart()
	{
	}

	public AbstractChart(AggregateResultPojo graphData)
	{
		this.graphData = graphData;
	}

	/**
	 * Setup a basic core chart with given Values this bar chart will be
	 * vertical for horizontal child class should partially override this method
	 * and gives Orientation Horizontal
	 * 
	 * @return concrete instance of chart
	 */
	public AbstractChart setupGraph()
	{

		Map<Double, String> mXTextLabels = new HashMap<Double, String>();

		values.add(graphData.getCounts());
		XYMultipleSeriesRenderer renderer = buildRenderer(mXTextLabels,
				new int[]
				// Color for bars
				{ Color.argb(255, 72, 118, 255) });
		int i = 0;
		// Values of graph for which we are drawing graph
		for (String graphVal : graphData.getRatings())
		{
			try
			{
				graphVal = graphVal + ",  "
						+ (int) graphData.getCounts().get(i).doubleValue();
				i++;
			} catch (IndexOutOfBoundsException e)
			{
				e.printStackTrace();
			}
			// label for that value of for graph
			renderer.getBarLabels().add(graphVal);
		}
	 
		renderer.setMargins(new int[]
		{ 25, 25, 25, 25 });
		renderer.setFitLegend(true);
		renderer.setInitialRange(new double[]{0,0,0,0},1);
		// renderer.setZoomEnabled(false);
		// renderer.setZoomEnabled(false, false);
		renderer.setBarSpacing(.2);
		// we are finding max value of graph that we what will be max height of
		// our graph so we are limiting our scale
		double max = Collections.max(graphData.getCounts());
		// renderer.setPanLimits(new double[] { -60, max, 0, 0 });
		renderer.setChartValuesTextSize(Device.getInstance().getPercentWidth(
				.07f));

		renderer.setPanEnabled(true, true);
		//renderer.setPanEnabled(true);
		// providing chart metadata like title of chart etc.
		setChartSettings(renderer, "", graphData.getTitle(), "", 1, graphData
				.getCounts().size(), 0, max, Color.GRAY, Color.LTGRAY);
		return this;
	}

	public List<List<Double>> getValues()
	{
		return values;
	}

	public String getTitle()
	{
		return title;
	}

	/**
	 * values of chart to be drawn.
	 * 
	 * @param values
	 */
	public void setValues(List<List<Double>> values)
	{
		this.values = values;
	}

	public void setTitle(String title)
	{
		this.title = title;
	}

	/**
	 * Builds an XY multiple dataset using the provided values.
	 * 
	 * @param titles
	 *            the series titles
	 * @param xValues
	 *            the values for the X axis
	 * @param yValues
	 *            the values for the Y axis
	 * @return the XY multiple dataset
	 */
	protected XYMultipleSeriesDataset buildDataset(String[] titles,
			List<double[]> xValues, List<double[]> yValues)
	{
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		addXYSeries(dataset, titles, xValues, yValues, 0);
		return dataset;
	}

	protected XYMultipleSeriesRenderer renderer;
	int[] colors = new int[]
	{ Color.BLUE /* , Color.BLUE */};

	public XYMultipleSeriesRenderer buildRenderer(
			Map<Double, String> mXTextLabels, int[] colors)
	{

		this.colors = colors;
		renderer = buildBarRenderer(colors);
		renderer.setTextLables(mXTextLabels);
		return renderer;

	}

	private List<List<Double>> values = new ArrayList<List<Double>>();
	private String title;

	public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles,
			List<double[]> xValues, List<double[]> yValues, int scale)
	{
		int length = titles.length;
		for (int i = 0; i < length; i++)
		{
			XYSeries series = new XYSeries(titles[i], scale);
			double[] xV = xValues.get(i);
			double[] yV = yValues.get(i);
			int seriesLength = xV.length;
			for (int k = 0; k < seriesLength; k++)
			{
				series.add(xV[k], yV[k]);
			}
			dataset.addSeries(series);
		}
	}

	/**
	 * Builds an XY multiple series renderer.
	 * 
	 * @param colors
	 *            the series rendering colors
	 * @param styles
	 *            the series point styles
	 * @return the XY multiple series renderers
	 */
	protected XYMultipleSeriesRenderer buildRenderer(int[] colors,
			PointStyle[] styles)
	{
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		setRenderer(renderer, colors, styles);
		return renderer;
	}

	protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors,
			PointStyle[] styles)
	{
		renderer.setAxisTitleTextSize(23);
		renderer.setChartTitleTextSize(23);
		renderer.setLabelsTextSize(23);
		renderer.setLegendTextSize(23);
		renderer.setPointSize(5f);
		renderer.setMargins(new int[]
		{ 25, 35, 20, 25 });
		int length = colors.length;
		for (int i = 0; i < length; i++)
		{
			XYSeriesRenderer r = new XYSeriesRenderer();
			r.setColor(colors[i]);
			r.setPointStyle(styles[i]);
			renderer.addSeriesRenderer(r);
		}
	}

	/**
	 * Sets a few of the series renderer settings.
	 * 
	 * @param renderer
	 *            the renderer to set the properties to
	 * @param title
	 *            the chart title
	 * @param xTitle
	 *            the title for the X axis
	 * @param yTitle
	 *            the title for the Y axis
	 * @param xMin
	 *            the minimum value on the X axis
	 * @param xMax
	 *            the maximum value on the X axis
	 * @param yMin
	 *            the minimum value on the Y axis
	 * @param yMax
	 *            the maximum value on the Y axis
	 * @param axesColor
	 *            the axes color
	 * @param labelsColor
	 *            the labels color
	 */
	public void setChartSettings(XYMultipleSeriesRenderer renderer,
			String title, String xTitle, String yTitle, double xMin,
			double xMax, double yMin, double yMax, int axesColor,
			int labelsColor)
	{
		title = xTitle;
		if(title.length()>40){
			title = title.substring(0,40).concat("....");
		}
		renderer.setChartTitle(title);
		renderer.setXTitle("");
		renderer.setYTitle("");
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);

		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}

	/**
	 * Builds a category series using the provided values.
	 * 
	 * @param titles
	 *            the series titles
	 * @param values
	 *            the values
	 * @return the category series
	 */
	protected CategorySeries buildCategoryDataset(String title, double[] values)
	{
		CategorySeries series = new CategorySeries(title);
		int k = 0;
		for (double value : values)
		{
			series.add("Project " + ++k, value);
		}

		return series;
	}

	/**
	 * Builds a bar multiple series dataset using the provided values.
	 * 
	 * @param titles
	 *            the series titles
	 * @param values
	 *            the values
	 * @return the XY multiple bar dataset
	 */
	protected XYMultipleSeriesDataset buildBarDataset(String[] titles,
			List<List<Double>> values)
	{
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		int length = titles.length;
		for (int i = 0; i < length; i++)
		{
			CategorySeries series = new CategorySeries(titles[i]);
			List<Double> v = values.get(i);

			for (Double d : v)
			{
				series.add(d);
			}

			dataset.addSeries(series.toXYSeries());
		}
		return dataset;
	}

	/**
	 * Builds a bar multiple series renderer to use the provided colors.
	 * 
	 * @param colors
	 *            the series renderers colors
	 * @return the bar multiple series renderer
	 */
	protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors)
	{
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		renderer.setAxisTitleTextSize(23);
		renderer.setChartTitleTextSize(23);
		renderer.setLabelsTextSize(23);
		renderer.setLegendTextSize(23);
		int length = colors.length;
		for (int i = 0; i < length; i++)
		{
			SimpleSeriesRenderer r = new SimpleSeriesRenderer();
			r.setColor(colors[i]);
			renderer.addSeriesRenderer(r);
		}
		return renderer;
	}

	public AggregateResultPojo getGraphData()
	{
		return graphData;
	}

	public void setGraphData(AggregateResultPojo graphData)
	{
		this.graphData = graphData;
	}

}
